package manager_web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import manager_service.service.BackResourceService;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import test_common.constant.Constant;
import test_common.entity.Manager;
import test_common.entity.Resource;
import test_common.entity.ResourceVo;
import test_common.utils.ContentIsNull;
import test_common.utils.ResponseResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author zhangzheng
 * @create 2021/1/8
 */

@Controller
@RequestMapping("/backResource")
public class BackResourceController {

    @Reference
    private BackResourceService backResourceService;

    @ResponseBody
    @RequestMapping("/addResource")
   public ResponseResult addResource(HttpServletRequest request, @RequestParam(value = "resource_file",required = false) MultipartFile file){
        ResponseResult result=new ResponseResult();
       String title=request.getParameter("title");
       String costType=request.getParameter("costType");
       String costNumber=request.getParameter("costNumber");
        String fileType=request.getParameter("fileType");
        String courseId=request.getParameter("courseId");
        String chapterId=request.getParameter("chapterId");
        String userId=request.getParameter("userId");
        Manager manager =(Manager) request.getSession().getAttribute("own");
        userId= String.valueOf(manager.getManagerid());
        if (ContentIsNull.isNull(title)||ContentIsNull.isNull(costType)||ContentIsNull.isNull(costNumber)||ContentIsNull.isNull(fileType)
    ||ContentIsNull.isNull(courseId)||ContentIsNull.isNull(chapterId)||ContentIsNull.isNull(userId)){
            result.setMessage("必填内容不可以为空");
            result.setStatus(Constant.RESPONSE_FAILED);
            return result;
        }

       String cp = "/upload/resource/";
       String realCp="F:/netclass/test_manager/manager_web/src/main/resources/static";
       String resourceUrl=null;
       String simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
       if(!file.isEmpty()){
           try {
               resourceUrl=cp+simpleDateFormat+file.getOriginalFilename();
               file.transferTo(new File(realCp+resourceUrl));
           } catch (IOException e) {
               e.printStackTrace();

           }
       }
       Resource resource=new Resource();
       resource.setClickCount(0);
       resource.setCourseId(Integer.parseInt(courseId));
       resource.setChapterId(Integer.parseInt(chapterId));
       resource.setCostNumber(Integer.parseInt(costNumber));
       resource.setCostType(Integer.parseInt(costType));
       resource.setCreateDate(new Date());
       resource.setFileType(fileType);
       resource.setOriginalName(file.getOriginalFilename());
       resource.setPath(resourceUrl);
       resource.setFileSize(String.valueOf( file.getSize()));
       resource.setUserId(Integer.parseInt(userId));
       resource.setTitle(title);
       resource.setStatus(Constant.Resource_ENABLE);


       backResourceService.addResource(resource);
       result.setStatus(Constant.RESPONSE_SUCCESS);
       result.setMessage("章节资源添加成功");
       return result;
   }


   @RequestMapping("/showResources")
    public ModelAndView showResources(HttpServletRequest request){
        ModelAndView mav=new ModelAndView();
        String courseId=request.getParameter("courseId");
        String chapterId=request.getParameter("chapterId");
        Manager manager =(Manager) request.getSession().getAttribute("own");
        String userId= String.valueOf(manager.getManagerid());
        List<ResourceVo> resourceVos=backResourceService.showResources(userId,chapterId,courseId);
        mav.setViewName("back_resourceSet");
        mav.addObject("resourceVos",resourceVos);
        mav.addObject("courseId",courseId);
        mav.addObject("chapterId",chapterId);
        return mav;
    }

    @ResponseBody
    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(HttpServletRequest request){
        ResponseResult result=new ResponseResult();
        String resourceId=request.getParameter("resourceId");
       backResourceService.modifyStatus(Integer.parseInt(resourceId));
       result.setStatus(Constant.RESPONSE_SUCCESS);
       result.setMessage("资源状态修改成功");

       return result;

    }

    @RequestMapping("/searchResources")
    public ModelAndView searchResources(HttpServletRequest request){
        ModelAndView mav=new ModelAndView();
        String title=request.getParameter("title");
        String userId=request.getParameter("userId");
        String status=request.getParameter("status");
        String fileType=request.getParameter("fileType");
        String dateStart=request.getParameter("dataStart");
        String dataEnd=request.getParameter("dateEnd");
        String courseId=request.getParameter("courseId");
        String chapterId=request.getParameter("chapterId");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (ContentIsNull.isNull(dateStart)){
            dateStart="2020-05-01 00:00:00";
        }
        if (ContentIsNull.isNull(dataEnd)){
            dataEnd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        }
        if (fileType.equals("-1")){
            fileType=null;
        }
        if (status.equals("-1")){
            status=null;
        }
        List<ResourceVo> resources=null;
        try {
          resources=backResourceService.searchResources(title,userId,status,fileType,
                    simpleDateFormat.parse(dateStart),
                    simpleDateFormat.parse(dataEnd),
                    courseId,chapterId);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        mav.addObject("resourcesVo",resources);
        mav.setViewName("back_resourceSearch_result");
        return mav;
    }

    @ResponseBody
    @RequestMapping("/selectResource")
    public ResponseResult selectResource(HttpServletRequest request){
        ResponseResult result=new ResponseResult();
        String id=request.getParameter("id");
        Resource resource=backResourceService.selectResource(id);
        if (resource.getStatus()==1){
            result.setStatus(Constant.RESPONSE_SUCCESS);
            result.setObject(resource);
        }
        else{
            result.setMessage("资源处于禁用状态，不可修改");
            result.setStatus(Constant.RESPONSE_FAILED);
        }

        return result;
    }

    @ResponseBody
    @RequestMapping("/modifyResource")
    public ResponseResult modifyResource(HttpServletRequest request,@RequestParam(value = "resource_file",required = false) MultipartFile file){
        ResponseResult result=new ResponseResult();
        String id=request.getParameter("resourceId");
        String title=request.getParameter("title");
        String costType=request.getParameter("costType");
        String costNumber=request.getParameter("costNumber");
        String fileType=request.getParameter("fileType");
        String courseId=request.getParameter("courseId");
        String chapterId=request.getParameter("chapterId");
        String userId=request.getParameter("userId");
        Manager manager =(Manager) request.getSession().getAttribute("own");
        userId= String.valueOf(manager.getManagerid());
        if (ContentIsNull.isNull(title)||ContentIsNull.isNull(costType)||ContentIsNull.isNull(costNumber)||ContentIsNull.isNull(fileType)
                ||ContentIsNull.isNull(courseId)||ContentIsNull.isNull(chapterId)||ContentIsNull.isNull(userId)){
            result.setMessage("必填内容不可以为空");
            result.setStatus(Constant.RESPONSE_FAILED);
            return result;
        }

        String cp = "/upload/resource/";
        String realCp="F:/netclass/test_manager/manager_web/src/main/resources/static";
        String resourceUrl=null;
        String simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        Resource resource=new Resource();
        if(!file.isEmpty()){
            try {
                resourceUrl=cp+simpleDateFormat+file.getOriginalFilename();
                file.transferTo(new File(realCp+resourceUrl));
                resource.setPath(resourceUrl);
                resource.setOriginalName(file.getOriginalFilename());
                resource.setFileSize(String.valueOf( file.getSize()));
            } catch (IOException e) {
                e.printStackTrace();

            }
        }

        resource.setCourseId(Integer.parseInt(courseId));
        resource.setChapterId(Integer.parseInt(chapterId));
        resource.setCostNumber(Integer.parseInt(costNumber));
        resource.setCostType(Integer.parseInt(costType));
        resource.setCreateDate(new Date());
        resource.setFileType(fileType);
        resource.setTitle(title);
        resource.setId(Integer.parseInt(id));

        backResourceService.modifyResource(resource);
        result.setStatus(Constant.RESPONSE_SUCCESS);
        result.setMessage("章节资源修改成功");
        return result;
    }

    @RequestMapping("/showResourceDetail")
    public ModelAndView showResourceDetail(HttpServletRequest request){
        ModelAndView mav=new ModelAndView();
        String id = request.getParameter("id");
        Resource resource=backResourceService.selectResource(id);
        mav.addObject("path",resource.getPath());
        mav.addObject("fileType",resource.getFileType());
        mav.addObject("courseId",resource.getCourseId());
        mav.addObject("chapterId",resource.getChapterId());
        mav.setViewName("back_resourceDetailSet");
        return mav;
    }
}
